<template>
  <div class="login_container">
    <div class="login_box">

      <!--   头像区   -->
      <div class="avatar_box">
        <img src="../assets/img/head.jpg" alt="">
      </div>
      <!--   登录表单   -->
      <el-form
          ref="loginFormRef"
          :rules="loginFormRules"
          :model="loginForm"
          class="el-form1">
        <el-form-item prop="username">
          <el-input
              v-model="loginForm.username"
              placeholder="Username"
              prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
              v-model="loginForm.password"

              type="password"
              placeholder="Password"
              prefix-icon="el-icon-key"></el-input>
        </el-form-item>

        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetLoginForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "axios";

export default {
  name: "Login",
  data() {

    return {
      loginForm: {
        username: '',
        password: ''
      },

      loginFormRules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 2, max: 16, message: '请输入2-16个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 16, message: '请输入6-16个字符', trigger: 'blur'}
        ],
      },

    }
  }, methods: {

    //点击重置表单
    resetLoginForm: function () {
      console.log(this)
      this.$refs.loginFormRef.resetFields()
    },
    login() {
      //表单预验证
      this.$refs.loginFormRef.validate(async (valid) => {
        //console.log(valid)
        //不通过
        if (!valid) {
          console.log('不通过')
          return
        }

        console.log('pass')

        const {data: res} = await axios.post('login', this.loginForm);

        // console.log(response.data.data)
        if (res.meta.status !== 200) {
          this.$message.error('登录失败')
          return
        }

        this.$message.success('登录成功')

        //save token
        window.sessionStorage.setItem('token', res.data.token)

        //route
        this.$router.push('/home')


      })

    }
  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #ffffff;
  border-radius: 25px;


  margin: auto;
  left: 0;
  top: 0;
  bottom: 0;
  right: 0;

  position: absolute;
  //left: 50%;
  //top: 50%;
  //transform: translate(-50%, -50%);
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  box-shadow: 0 0 10px #000;
  margin: 0 auto;
  transform: translate(0, -50%);
  padding: 5px;
  background-color: #eeeeee;

  img {
    //border:1px solid #eee;
    border-radius: 50%;
    width: 100%;
    height: 100%;
    background-color: #eee;
  }

}

.btns {
  display: flex;
  justify-content: right;
}

.el-form1 {
  margin: auto;
  bottom: 0;
  left: 0;
  right: 0;

  width: 80%;
  position: absolute;
  box-sizing: border-box;
}
</style>